
 
<html> 
 
<head> 
<title>Contour by WebGL</title> 
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> 
 
    <script type="text/javascript" src="data.js"></script> 
    <script type="text/javascript" src="ele.js"></script> 
    <script type="text/javascript" src="vertices.js"></script> 
    <script type="text/javascript" src="contourPoints.js"></script>
 
<script type="text/javascript" src="glMatrix-0.9.5.min.js"></script> 
<script type="text/javascript" src="webgl-utils.js"></script> 
 
<script id="shader-fs" type="x-shader/x-fragment"> 
    #ifdef GL_ES
    precision highp float;
    #endif
 
    varying vec4 vColor;
 
    void main(void) {
        //gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
        gl_FragColor = vColor;
    }
</script> 
 
<script id="shader-vs" type="x-shader/x-vertex"> 
    attribute vec3 aVertexPosition;
    attribute vec4 aVertexColor;
 
    uniform mat4 uMVMatrix;
    uniform mat4 uPMatrix;
 
    varying vec4 vColor;
 
    void main(void) {
        gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
        vColor = aVertexColor;
    }
</script> 
 
 
<script type="text/javascript"> 
 
    var gl;
 
    function initGL(canvas) {
        try {
            gl = canvas.getContext("experimental-webgl");
            gl.viewportWidth = canvas.width;
            gl.viewportHeight = canvas.height;
        } catch (e) {
        }
        if (!gl) {
            alert("Could not initialise WebGL, sorry :-(");
        }
    }
 
 
    function getShader(gl, id) {
        var shaderScript = document.getElementById(id);
        if (!shaderScript) {
            return null;
        }
 
        var str = "";
        var k = shaderScript.firstChild;
        while (k) {
            if (k.nodeType == 3) {
                str += k.textContent;
            }
            k = k.nextSibling;
        }
 
        var shader;
        if (shaderScript.type == "x-shader/x-fragment") {
            shader = gl.createShader(gl.FRAGMENT_SHADER);
        } else if (shaderScript.type == "x-shader/x-vertex") {
            shader = gl.createShader(gl.VERTEX_SHADER);
        } else {
            return null;
        }
 
        gl.shaderSource(shader, str);
        gl.compileShader(shader);
 
        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            alert(gl.getShaderInfoLog(shader));
            return null;
        }
 
        return shader;
    }
 
 
    var shaderProgram;
 
    function initShaders() {
        var fragmentShader = getShader(gl, "shader-fs");
        var vertexShader = getShader(gl, "shader-vs");
 
        shaderProgram = gl.createProgram();
        gl.attachShader(shaderProgram, vertexShader);
        gl.attachShader(shaderProgram, fragmentShader);
        gl.linkProgram(shaderProgram);
 
        if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
        }
 
        gl.useProgram(shaderProgram);
 
        shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
        gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
 
        shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
        gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
 
        shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
        shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
    }
 
 
    var mvMatrix = mat4.create();
    var mvMatrixStack = [];
    var pMatrix = mat4.create();
 
    function mvPushMatrix() {
        var copy = mat4.create();
        mat4.set(mvMatrix, copy);
        mvMatrixStack.push(copy);
    }
 
    function mvPopMatrix() {
        if (mvMatrixStack.length == 0) {
            throw "Invalid popMatrix!";
        }
        mvMatrix = mvMatrixStack.pop();
    }
 
 
    function setMatrixUniforms() {
        gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
        gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
    }
 
 
    function degToRad(degrees) {
        return degrees * Math.PI / 180;
    }
 
 
    var cupVertexPositionBuffer;
    var cupVertexColorBuffer;
    var vertices = [];
    var count;
    
    var contourVertexPositionBuffer;
    var contourVertexColorBuffer;
    var contourVertices = [];
    var contourVerticesCount;
    
    function cupVertices(){ 
    	z = 50;
    	xx = 0;
    	yy = 4;
    	zz = -5;   	
        var k;
        count = eleCount;
        // cup vertices
        for (var i=0; i < count; i++) {
        	// Point 1
        	k = ele[i * 4 + 1];
        	vertices[i * 9 + 0] = verticesData[k * 4 + 1] / z + xx;
        	vertices[i * 9 + 1] = verticesData[k * 4 + 2] / z + yy;
        	vertices[i * 9 + 2] = verticesData[k * 4 + 3] / z + zz;        	
        	// Point 2
        	k = ele[i * 4 + 2];
        	vertices[i * 9 + 3] = verticesData[k * 4 + 1] / z + xx;
        	vertices[i * 9 + 4] = verticesData[k * 4 + 2] / z + yy;
        	vertices[i * 9 + 5] = verticesData[k * 4 + 3] / z + zz;        	
        	// Point 3
        	k = ele[i * 4 + 3];
        	vertices[i * 9 + 6] = verticesData[k * 4 + 1] / z + xx;
        	vertices[i * 9 + 7] = verticesData[k * 4 + 2] / z + yy;
        	vertices[i * 9 + 8] = verticesData[k * 4 + 3] / z + zz;
        };
        // contourVertices
        contourVerticesCount = contourLineCount;
    	for (var i=0; i < contourVerticesCount; i++) {
        	// Point 1
        	contourVertices[i * 6 + 0] = contourLine[i * 7 + 1] / z + xx;
        	contourVertices[i * 6 + 1] = contourLine[i * 7 + 2] / z + yy;
        	contourVertices[i * 6 + 2] = contourLine[i * 7 + 3] / z + zz;
        	// Point 2	
        	contourVertices[i * 6 + 3] = contourLine[i * 7 + 4] / z + xx;
        	contourVertices[i * 6 + 4] = contourLine[i * 7 + 5] / z + yy;
        	contourVertices[i * 6 + 5] = contourLine[i * 7 + 6] / z + zz;
		};
    }
 
    function initBuffers() { 
        cupVertices();
        
    	// cup
        cupVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, cupVertexPositionBuffer);        
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
        cupVertexPositionBuffer.itemSize = 3;
        cupVertexPositionBuffer.numItems = count * 3;
        
        cupVertexColorBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, cupVertexColorBuffer);
        var colors = [];
        for (var i=0; i < count * 3; i++) {
            colors[i * 4 + 0] = 0.0;
            colors[i * 4 + 1] = 0.0;
            colors[i * 4 + 2] = 1.0;
            colors[i * 4 + 3] = 1.0;
        }
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
        cupVertexColorBuffer.itemSize = 4;
        cupVertexColorBuffer.numItems = count * 3;
        
        // contour
        contourVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, contourVertexPositionBuffer);        
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(contourVertices), gl.STATIC_DRAW);
        contourVertexPositionBuffer.itemSize = 3;
        contourVertexPositionBuffer.numItems = contourVerticesCount * 2;
        
        contourVertexColorBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, contourVertexColorBuffer);
        colors = [];
        for (var i=0; i < contourVerticesCount * 2; i++) {
            colors[i * 4 + 0] = 1.0;
            colors[i * 4 + 1] = 0.0;
            colors[i * 4 + 2] = 0.0;
            colors[i * 4 + 3] = 1.0;
        }
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
        contourVertexColorBuffer.itemSize = 4;
        contourVertexColorBuffer.numItems = contourVerticesCount * 2;
    }
 
 
    var rCup = 0;
 
    function drawScene() {
        gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
 
        mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.001, 100.0, pMatrix);
 
        mat4.identity(mvMatrix);
		
		//cup
        mat4.translate(mvMatrix, [0.8, 0.0, -8.0]);
 		/*
        mvPushMatrix();
        mat4.rotate(mvMatrix, degToRad(rCup), [0, 1, 0]);
 
        gl.bindBuffer(gl.ARRAY_BUFFER, cupVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, cupVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); 
        gl.bindBuffer(gl.ARRAY_BUFFER, cupVertexColorBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, cupVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0); 
 
        setMatrixUniforms();
        gl.drawArrays(gl.TRIANGLES, 0, cupVertexPositionBuffer.numItems);
 
        mvPopMatrix();

        
        //contour
        mat4.translate(mvMatrix, [0.0, 0.0, 0.0]); 		
        */
        mvPushMatrix();
        mat4.rotate(mvMatrix, degToRad(rCup), [0, 1, 0]);
 
        gl.bindBuffer(gl.ARRAY_BUFFER, contourVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, contourVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.bindBuffer(gl.ARRAY_BUFFER, contourVertexColorBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, contourVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0); 
 
        setMatrixUniforms();
        gl.drawArrays(gl.LINES, 0, contourVertexPositionBuffer.numItems);
 
        mvPopMatrix();
    }
 
 
    var lastTime = 0;
 
    function animate() {
        var timeNow = new Date().getTime();
        if (lastTime != 0) {
            var elapsed = timeNow - lastTime;
 
            //rCup += (90 * elapsed) / 1000.0;
            rCup = 90;
        }
        lastTime = timeNow;
    }
 
 
    function tick() {
        requestAnimFrame(tick);
        drawScene();
        animate();
    }
 
 
    function webGLStart() {
        var canvas = document.getElementById("contour");
        initGL(canvas);
        initShaders()
        initBuffers();
 
        gl.clearColor(1.0, 1.0, 1.0, 1.0);
        gl.enable(gl.DEPTH_TEST);
 
        tick();
        //drawScene();
    }
 
</script> 
 
 
</head> 
 
 
<body onload="webGLStart();"> 
    <center><h1>Contour by WebGL</h1></center>
 
    <center><canvas id="contour" style="border: none;" width="640" height="480"></canvas></center>
 
 
</body> 
 
</html> 